const { error, success } = require('../utils/response.js')


module.exports = (method, schema) => {
  async function validateParams(ctx, next) {
    let data
    if (method === 'get') {
      data = ctx.request.query
    } else {
      data = ctx.request.body
    }
    const validate = schema.validate(data).error
    if (validate) {
      error(ctx, validate.details)
      return
    }
    await next()
  }
  return validateParams
}